<template>
  <el-dialog
    :title="!dataForm.pid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="患者姓名" prop="pname">
      <el-input v-model="dataForm.pname" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="psex">
      <el-input v-model="dataForm.psex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="page">
      <el-input v-model="dataForm.page" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="入院时间" prop="prtime">
      <el-input v-model="dataForm.prtime" placeholder="入院时间"></el-input>
    </el-form-item>
    <el-form-item label="出院时间" prop="pctime">
      <el-input v-model="dataForm.pctime" placeholder="出院时间"></el-input>
    </el-form-item>
    <el-form-item label="症状" prop="ptype">
      <el-input v-model="dataForm.ptype" placeholder="症状"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pid: 0,
          pname: '',
          psex: '',
          page: '',
          prtime: '',
          pctime: '',
          ptype: ''
        },
        dataRule: {
          pname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          psex: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          page: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          prtime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pctime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ptype: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/patient/info/${this.dataForm.pid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pname = data.patient.pname
                this.dataForm.psex = data.patient.psex
                this.dataForm.page = data.patient.page
                this.dataForm.prtime = data.patient.prtime
                this.dataForm.pctime = data.patient.pctime
                this.dataForm.ptype = data.patient.ptype
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/patient/${!this.dataForm.pid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pid': this.dataForm.pid || undefined,
                'pname': this.dataForm.pname,
                'psex': this.dataForm.psex,
                'page': this.dataForm.page,
                'prtime': this.dataForm.prtime,
                'pctime': this.dataForm.pctime,
                'ptype': this.dataForm.ptype
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
